#include "atomic.pb.h"
#include "zrpc/base/logger.h"
#include "zrpc/zrpc/rpc_server.h"

class AtomicServiceImpl : public ::example::AtomicService {
public:
	virtual void Get(::google::protobuf::RpcController* controller,
		const ::example::GetRequest* request,
		::example::AtomicResponse* response,
		::google::protobuf::Closure* done) {

		response->set_success(true);
		response->set_id(request->id());

		auto it = value_map_.lower_bound(request->id());
		if (it == value_map_.end()) {
			response->set_old_value(0);
			response->set_new_value(0);
		}
		else {
			response->set_old_value(it->second);
			response->set_new_value(it->second);
		}
		done->Run();
	}

	virtual Exchange() {}

private:
	std::unordered_map<int64_t, int64_t> value_map_;
};

int main() {
	AtomicServiceImpl impl;
	zrpc::EventLoop loop;
	zrpc::RpcServer server(&loop, "127.0.0.1", 6379);
	server.RegisterService(&impl);
	server.Start();
	loop.Run();
}